package pers.kw.job.server.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.kw.dozer.BeanMapper;
import pers.kw.entity.PageDto;
import pers.kw.job.server.dto.log.QueryJobLogReqDto;
import pers.kw.job.server.dto.log.SysJobLogDto;
import pers.kw.job.server.persist.dao.SysJobLogDao;
import pers.kw.job.server.persist.entity.SysJobLogDO;
import pers.kw.job.server.persist.mapper.SysJobLogMapper;
import pers.kw.utils.DoWrapUtil;
import pers.kw.utils.poi.ExcelUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author ruoyi
 */
@Slf4j
@Service
public class SysJobLogService {

    @Resource
    private SysJobLogMapper sysJobLogMapper;

    @Resource
    private SysJobLogDao sysJobLogDao;

    @Resource
    private BeanMapper beanMapper;

    /**
     * 导出日志
     *
     * @param response
     * @param reqDto
     */
    public void export(HttpServletResponse response, QueryJobLogReqDto reqDto) {
        List<SysJobLogDto> list = sysJobLogMapper.selectJobLogList(reqDto);
        ExcelUtil<SysJobLogDto> util = new ExcelUtil<>(SysJobLogDto.class);
        util.exportExcel(response, list, "调度日志");
    }

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param reqDto 调度日志信息
     * @return 调度任务日志集合
     */
    public PageDto<SysJobLogDto> selectJobLogList(QueryJobLogReqDto reqDto) {
        log.info("分页查询job-log列表，入参:{}", JSON.toJSONString(reqDto));
        Page<SysJobLogDto> selectResult = PageHelper.startPage(reqDto.getPageNum(), reqDto.getPageSize());
        sysJobLogMapper.selectJobLogList(reqDto);
        PageDto<SysJobLogDto> page = DoWrapUtil.page(selectResult);
        log.info("分页查询job-log列表，出参:{}", JSON.toJSONString(page));
        //PageHelper.clearPage();
        return page;
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param bizCode 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    public SysJobLogDto getJobLogByBizCode(String bizCode) {
        return beanMapper.convert(sysJobLogDao.findByBizCode(bizCode), SysJobLogDto.class);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void addJobLog(SysJobLogDto jobLog) {
        SysJobLogDO sysJobLogDO = new SysJobLogDO();
        sysJobLogDO.setJobName(jobLog.getJobName());
        sysJobLogDO.setJobGroup(jobLog.getJobGroup());
        sysJobLogDO.setInvokeTarget(jobLog.getInvokeTarget());
        sysJobLogDO.setJobMessage(jobLog.getJobMessage());
        sysJobLogDO.setStatus(jobLog.getStatus());
        sysJobLogDO.setExceptionInfo(jobLog.getExceptionInfo());
        sysJobLogDO.setStartTime(jobLog.getStartTime());
        sysJobLogDO.setStopTime(jobLog.getStopTime());
        DoWrapUtil.insert(sysJobLogDO, false, false);
        sysJobLogDao.save(sysJobLogDO);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    public void deleteJobLogByIds(Long[] logIds) {
        sysJobLogMapper.deleteJobLogByIds(logIds);
    }

    /**
     * 删除任务日志
     *
     * @param jobId 调度日志ID
     */
    public void deleteJobLogById(Long jobId) {
        sysJobLogMapper.deleteJobLogById(jobId);
    }

    /**
     * 清空任务日志
     */
    public void cleanJobLog() {
        sysJobLogMapper.cleanJobLog();
    }
}
